package game.Player;

import game.Configuration;
import game.Helper;
import game.Scene;
import game.Vector3;
import javafx.scene.Camera;
import javafx.scene.input.KeyCode;

import java.awt.*;
import java.awt.event.KeyEvent;

//Controller for the human player
public class PlayerController extends Controller {
    KeyCode up;
    KeyCode down;
    KeyCode left;
    KeyCode right;
    KeyCode reload;
    KeyCode pickUp;
    KeyCode primary;
    KeyCode secondary;
    KeyCode knife;
    KeyCode alternate;
    KeyCode store;
    KeyCode plantKey;

    /**
     * Constructor.
     *
     * @param player Player controlled by the controller.
     */
    public PlayerController(Player player) throws Exception {
        super(player);

    }

    /**
     * Called on every frame, updates player position and rotation.
     *
     * @param deltaTime Time passed since last frame was rendered.
     */
    public void update(double deltaTime) {
        //Update keys
        up = KeyCode.getKeyCode(Configuration.getUp());
        down = KeyCode.getKeyCode(Configuration.getDown());
        left = KeyCode.getKeyCode(Configuration.getLeft());
        right = KeyCode.getKeyCode(Configuration.getRight());
        reload = KeyCode.getKeyCode(Configuration.getReload());
        pickUp = KeyCode.getKeyCode(Configuration.getPickUp());
        primary = KeyCode.getKeyCode(Configuration.getPrimaryWeapon());
        secondary = KeyCode.getKeyCode(Configuration.getSecondaryWeapon());
        knife = KeyCode.getKeyCode(Configuration.getKnife());
        alternate = KeyCode.getKeyCode(Configuration.getalternateWeapon());
        store = KeyCode.getKeyCode(Configuration.getStore());
        plantKey = KeyCode.getKeyCode(Configuration.getPlant());

        if (this.player.isAlive()) {
            Point mouseWindowPos = Helper.mousePosInWindow();

            Point mouseVector = new Point(mouseWindowPos.x - Helper.screenWidth() / 2,
                    mouseWindowPos.y - Helper.screenHeight() / 2);

            double vectorDist = Math.sqrt(mouseVector.x * mouseVector.x + mouseVector.y * mouseVector.y);
            Vector3 mouseDir = new Vector3((double) mouseVector.x / vectorDist, (double) mouseVector.y / vectorDist, 0.0);
            this.player.setRotation(Math.atan2(mouseDir.y, mouseDir.x));

            if (!this.player.isPlanting()) {
                if (Input.getKey(KeyCode.LEFT) || Input.getKey(left)) {
                    //System.out.println("IN");
                    this.player.playerSpeed.x = -1.0;
                } else if (Input.getKey(KeyCode.RIGHT) || Input.getKey(right)) {
                    //System.out.println("IN");
                    this.player.playerSpeed.x = +1.0d;
                } else {
                    this.player.playerSpeed.x = 0;
                }

                if (Input.getKey(KeyCode.UP) || Input.getKey(up)) {
                    //System.out.println("IN");
                    this.player.playerSpeed.y = -1.0;
                } else if (Input.getKey(KeyCode.DOWN) || Input.getKey(down)) {
                    //System.out.println("IN");
                    this.player.playerSpeed.y = +1.0;
                } else {
                    this.player.playerSpeed.y = 0;
                }
                this.player.playerSpeed = this.player.playerSpeed.normalized().multiply(player.runSpeed);
            }

            // Recarga
            if (Input.getKey(reload)) {
                this.player.tryToReload();
            }

            // PickUp weapon from the ground
            if (Input.getKeyClick(pickUp)) {
                this.player.tryPickUpWeapon();
            }

            if (Input.getKeyClick(alternate)) {
                this.player.tryToAtlernate();
            }

            //TODO: Plantado bomba definitivo
            if (Input.getKey(plantKey)) {
                this.player.tryToPlantBomb();
            } else {
                this.player.stopPlanting();
            }
            //TODO: Desactivado bomba definitivo
            if (Input.getKey(KeyCode.F)) {
                this.player.tryToDefuseBomb();
            } else {
                this.player.stopDefusing();
            }


            if (Helper.myCamera != null && !Helper.myCamera.isStoreOpen() && !Helper.myCamera.isEscMenuOpen()) {
                if (Input.getKeyClick(store)) {
                    if (player.isInBuyingTime() && player.isInBuyingZone())
                        Helper.myCamera.setStoreOpen(true);
                    else
                        Helper.myCamera.setShopError();
                }
                if (Input.getKeyClick((primary))) {
                    this.player.changeToPrimary();
                }
                if (Input.getKeyClick((secondary))) {
                    this.player.changeToSecondary();
                }
                if (Input.getKeyClick((knife))) {
                    this.player.changeToKnife();
                }
                if (Input.getMouse()) {
                    try {
                        this.player.shoot();
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            } else {
                if (Input.getKeyClick((KeyCode.DIGIT0))) {
                    Helper.myCamera.setStoreOpen(false);
                }
                if (Input.getKeyClick((KeyCode.DIGIT1))) {
                    Helper.myCamera.numberPress(1);
                }
                if (Input.getKeyClick((KeyCode.DIGIT2))) {
                    Helper.myCamera.numberPress(2);
                }
                if (Input.getKeyClick((KeyCode.DIGIT3))) {
                    Helper.myCamera.numberPress(3);
                }
                if (Input.getKeyClick((KeyCode.DIGIT4))) {
                    Helper.myCamera.numberPress(4);
                }
                if (Input.getKeyClick((KeyCode.DIGIT5))) {
                    Helper.myCamera.numberPress(5);
                }
                if (Input.getKeyClick((KeyCode.DIGIT6))) {
                    Helper.myCamera.numberPress(6);
                }
                if (Input.getKeyClick((KeyCode.DIGIT7))) {
                    Helper.myCamera.numberPress(7);
                }
                if (Input.getKeyClick((KeyCode.DIGIT8))) {
                    Helper.myCamera.numberPress(8);
                }
                if (Input.getMouseClick()) {
                    Helper.myCamera.notifyClick(mouseWindowPos.x, mouseWindowPos.y);
                }
            }
        }

    }

    public void notifyRoundStart() {
    }

    public void notifyDamageDone(Player inflictor) {

    }

}
